<?php
/*
	提现
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
global $_RQ;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
cfc('core')->checklogin();
if($_RQ['op']=='get'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid']));
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$sale_ids=$this->getall('member',array('dian_mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'mid','mid');
	$data['yewunum']=count($sale_ids);
	if($sale_ids){
		$sale_ids=array_keys($sale_ids);
	}
	$sale_ids[]=$_SESSION['uid'];
	$data['allmoney']=$this->getsum('order',array('sale_id'=>$sale_ids),'order_price');
	$data['dayfee']=$this->getsum('order',array('sale_id'=>$sale_ids,'createtime >'=>$daytime),'order_price');
	$data['tixian']=$member['credit2'];
	$data['commission_total']=pdo_getsum('jy_area_fee_commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'money');
	$data['xunifee']=$member['credit'.$this->setting['bufee_type']];
	$data['daixunifee']=$this->getsum('feelog',array('feetype'=>$this->setting['bufee_type'],'dian_mid'=>$_SESSION['uid'],'status'=>1),'money');
	$data['daixunifee']=$data['daixunifee']?:'0.00';
	$data['daifee']=$this->getsum('feelog',array('feetype'=>2,'dian_mid'=>$_SESSION['uid'],'status'=>1),'money');
	$data['daifee']=$data['daifee']?:'0.00';
	$return['data']=$data;
	$return['member']=$member;
	$member=$this->get('member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$level=$this->get('level',array('id'=>$member['level_id']));
	$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid'],'level >'=>$level['level']),
			'orderby'=>'level asc',
			'limit'=>'1',
	);
	$levels=$this->get_list('level',$parameter);
	$next_level=$levels[0];
	if($level['id']){
		$commissions=json_decode($level['commissions'],true);
		$tips='您当前等级是《'.$level['title'].'》,享受直推佣金['.$commissions['commission_0'].'],享受间推佣金['.$commissions['commission_0'].'],享受上级佣金['.$commissions['shangji'].']（不同的商品，所有差异！）';
	}else{
		$commissions=json_decode($next_level['commissions'],true);
		$tips='您当前等级是《默认等级》，再推荐['.$next_level['fennum'].']人,可升级为《'.$next_level['title'].'》,享受直推佣金['.$commissions['commission_0'].'],享受间推佣金['.$commissions['commission_0'].'],享受上级佣金['.$commissions['shangji'].']（不同的商品，所有差异！）';
	}
	$return['tips']=$tips;
	$return['level']=$level?:array();
	$return['params']['type']=$_RQ['type']?:0;
	$return['xunifeetitle']=$this->credit_type[$this->setting['bufee_type']];
	if($this->setting['withdrawtype']){
		$withdrawtype=array();
		$check=strstr($this->setting['withdrawtype'],'credit');
		if($check){
			$withdrawtype['credit']=$this->pay_types['credit'];
			$pay_type='credit';
		}
		$check=strstr($this->setting['withdrawtype'],'wechat');
		if($check){
			$withdrawtype['wechat']=$this->pay_types['wechat'];
			$pay_type=$pay_type?:'wechat';
		}
		$check=strstr($this->setting['withdrawtype'],'alipay');
		if($check){
			$withdrawtype['alipay']=$this->pay_types['alipay'];
			$pay_type=$pay_type?:'alipay';
		}
		$check=strstr($this->setting['withdrawtype'],'union');
		if($check){
			$withdrawtype['union']=$this->pay_types['union'];
			$pay_type=$pay_type?:'union';
		}
		$return['pay_types']=$withdrawtype;
	}else{
		$return['pay_types']=$this->pay_types;
		$pay_type='credit';
	}
	$return['post']=array('pay_type'=>$pay_type);
	set_html('title','我的资金');
	exi($return);
}
if($_RQ['op']=='post'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid']));
	$post=get_postdata();
	if(!$post['fee']){
		exi('请输入转账金额！','erroe');
	}
	if(!$post['mid']){
		exi('请输入身份ID！','erroe');
	}
	if($post['type']=='credit2'){
		if($member['credit2']<$post['fee']){
			exi('转账额度不能大于，当前余额！','erroe');
		}
		$type='credit2';
	}
	if($post['type']=='xuni'){
		if($member['credit'.$this->setting['bufee_type']]<$post['fee']){
			exi('转账额度不能大于，当前补偿金余额！','erroe');
		}
		$type='credit'.$this->setting['bufee_type'];
	}
	$tomember=pdo_get('core_members',array('id'=>$post['mid']));
	$tomid=$tomember['id'];
	if(!$tomid){
		exi('没找到收款人，转账失败！','erroe');
	}
	model('core/member')->member_credit($tomid,$type,$post['fee'],true,'jy_area_fee','上级转账');
	model('core/member')->member_credit($_SESSION['uid'],$type,$post['fee'],false,'jy_area_fee','转账给下级');					
	$params=array(
			'mid'=>$_SESSION['uid'],
			'dian_mid'=>$_SESSION['uid'],
			'msg'=>'转账给下级',
			'fee'=>$post['fee'],
			'status'=>1,
	);
	$this->set_log($params,0);
	exi('转账成功！');
}
if($_RQ['op']=='password'){
	$post=get_postdata();
	if(!$post['password']){
		exi('请输入支付密码！','erroe');
	}
	pdo_update('core_members',array('paypassword'=>$post['password']),array('id'=>$_SESSION['uid']));
	exi('设置成功！');
}
if($_RQ['op']=='feelist'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	if($_RQ['type']<=1){
		$feetype=2;
	}else{
		$feetype=$this->setting['bufee_type']?:3;
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>$feetype);
	$params['is_add']=$_RQ['type']==1 || $_RQ['type']==3?:0;
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('core_credit_log',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($return['list'] as &$v){
			$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
			$v['typetitle']=$this->feelog_type[$v['type']];
			$v['statustitle']=$this->commission_status[$v['status']];
	}
	exi($return);
}
if($_RQ['op']=='set'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$member_update=array(
			'downdata'=>ijson_encode($post['downdata']),
		);
		$this->pdo_update('member',$member_update,array('mid'=>$_SESSION['uid']));
		exi('操作成功！','','/myfee/');
	}
	$item=$this->pdo_get('member',array('mid'=>$_SESSION['uid']));
	$item['downdata']=$item['downdata']?json_decode($item['downdata'],true):array('down'=>array(array('num'=>5)));
	$item['downdata']['down']=$item['downdata']['down']?:array(array('num'=>5));
	$return['item']=$item;
	exi($return);
}
//结算
if($_RQ['op']=='balance'){
	$post=get_postdata();
	$post['feetype']=$post['feetype']==1?$this->setting['bufee_type']:2;
	$this->model('commission')->create_feebalance($_SESSION['uid'],$post);
	exi('申请结算成功！','','main');
}